package net.lab1024.smartadmin.module.support.imgFile.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.support.file.constant.FileServiceTypeEnum;
import net.lab1024.smartadmin.module.support.file.domain.vo.UploadVO;
import net.lab1024.smartadmin.module.support.file.service.IFileService;
import net.lab1024.smartadmin.module.support.imgFile.dao.ImgFileDao;
import net.lab1024.smartadmin.module.support.imgFile.domain.dto.ImgFileAddDTO;
import net.lab1024.smartadmin.module.support.imgFile.domain.dto.ImgFileUpdateDTO;
import net.lab1024.smartadmin.module.support.imgFile.domain.dto.ImgFileQueryDTO;
import net.lab1024.smartadmin.module.support.imgFile.domain.entity.ImgFileEntity;
import net.lab1024.smartadmin.module.support.imgFile.domain.vo.ImgFileUploadVO;
import net.lab1024.smartadmin.module.support.imgFile.domain.vo.ImgFileVO;
import net.lab1024.smartadmin.module.support.imgFile.domain.vo.ImgFileExcelVO;
import net.lab1024.smartadmin.util.SmartPageUtil;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * [  ]
 *
 * @author wldd
 * @version 1.0
 * @company 图片
 * @copyright (c)  图片Inc. All rights reserved.
 * @date 2020-12-29 20:51:26
 * @since JDK1.8
 */
@Service
public class ImgFileService {

    @Autowired
    private ImgFileDao imgFileDao;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;





    /**
     * 根据id查询
     */
    public ImgFileEntity getById(Long id){
        return  imgFileDao.selectById(id);
    }

    /**
     * 分页查询
     * @author wldd
     * @date 2020-12-29 20:51:26
     */
    public ResponseDTO<PageResultDTO<ImgFileVO>> queryByPage(ImgFileQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        IPage<ImgFileVO> voList = imgFileDao.queryByPage(page, queryDTO);
        PageResultDTO<ImgFileVO> pageResultDTO = SmartPageUtil.convert2PageResult(voList);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 添加
     * @author wldd
     * @date 2020-12-29 20:51:26
     */
    public ResponseDTO<String> add(ImgFileAddDTO addDTO) {
        ImgFileEntity entity = SmartBeanUtil.copy(addDTO, ImgFileEntity.class);
        imgFileDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * 编辑
     * @author wldd
     * @date 2020-12-29 20:51:26
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(ImgFileUpdateDTO updateDTO) {
        ImgFileEntity entity = SmartBeanUtil.copy(updateDTO, ImgFileEntity.class);
        imgFileDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author wldd
     * @date 2020-12-29 20:51:26
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        imgFileDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 查询全部导出对象
     * @author wldd
     * @date 2020-12-29 20:51:26
     */
    public List<ImgFileExcelVO> queryAllExportData(ImgFileQueryDTO queryDTO) {
        return imgFileDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author wldd
     * @date 2020-12-29 20:51:26
     */
    public List<ImgFileExcelVO> queryBatchExportData(List<Long> idList) {
        return imgFileDao.queryBatchExportData(idList);
    }

    public ResponseDTO<ImgFileUploadVO> fileUpload(MultipartFile file, Integer moduleType) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest("tourism-pro", "wldd", new File("file"));
        // 上传文件。
        ossClient.putObject(putObjectRequest);

        return null;
    }
}
